-module(prj2server).
-export([start/0, loop/1]).

start() ->
	register(prj2server, spawn(prj2server, loop, [self()])),
	receive Msg -> Msg end.

loop(Pid) ->
	io:format("~n" ++ atom_to_list(node()) ++ "\tWaiting for message..."),
	receive
		{start, Num, Proc} ->
			io:format("~n" ++ atom_to_list(node()) ++ "\tStarting servers"),			
			% start the number of servers on this node
			Servers = db:startServers(Num),
			
			% Message the list of servers back to the requesting client
			Proc ! Servers,
			
			% Restart loop
			loop(Pid);
		quit ->
			io:format("~n" ++ atom_to_list(node()) ++ "\tExiting"),
			Pid ! quit;
		Other ->
			io:format("Unknown msg received\t~p~n", [Other])
	end.
	